package com.sskj.ffex.p.activity;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.sskj.ffex.MyAppLication;
import com.sskj.ffex.m.HttpConfig;
import com.sskj.ffex.m.bean.CoinEntry;
import com.sskj.ffex.m.bean.ExchangeCoin;
import com.sskj.ffex.m.bean.HttpData;
import com.sskj.ffex.m.bean.UserInfo;
import com.sskj.ffex.m.http.JsonCallBack;
import com.sskj.ffex.p.base.BasePresenter;
import com.sskj.ffex.utils.ToastUtil;
import com.sskj.ffex.v.activity.ExchangeActivity;

import java.util.List;

public class ExchangePresenter extends BasePresenter<ExchangeActivity> {
    public void getUserInfo() {
        OkGo.<HttpData<UserInfo>>post(HttpConfig.BASE_URL + HttpConfig.GET_USER_INFO)
                .params("mobile", MyAppLication.getMobile())
                .execute(new JsonCallBack<HttpData<UserInfo>>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData<UserInfo>> response) {
                        HttpData<UserInfo> httpData = response.body();
                        if (httpData.getStatus() == 200) {
                            mView.updateUserInfo(httpData.getData());
                        }
                    }
                });
    }

    /**
     * 获取可兑换币种
     */
    public void getExchangeCoinList() {
        OkGo.<HttpData<List<CoinEntry>>>get(HttpConfig.BASE_URL + HttpConfig.EXCHANE_COIN_LIST1)
                .execute(new JsonCallBack<HttpData<List<CoinEntry>>>(this) {
                    @Override
                    protected void _onSuccess(Response<HttpData<List<CoinEntry>>> response) {
                        if (response.body().status == 200) {
                            mView.setExchangeAbleList(response.body().getData());
                        } else {
                            ToastUtil.showShort(response.body().getMsg());
                        }
                    }
                });
    }

    /**
     * 可兑换币种列表
     *
     * @param coin
     */
    public void getCoinList(String coin) {
        OkGo.<HttpData<List<ExchangeCoin>>>get(HttpConfig.BASE_URL + HttpConfig.EXCHANE_COIN_LIST)
                .params("coin", coin)
                .execute(new JsonCallBack<HttpData<List<ExchangeCoin>>>(this) {
                    @Override
                    protected void _onSuccess(Response<HttpData<List<ExchangeCoin>>> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getStatus() == 200) {
                            mView.setExchangeCoinData(response.body().getData());
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }
                });
    }


    public void exchange(String coin, String ex_coin, String num, String tpwd) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.EXCHANE_COIN)
                .params("coin", coin)
                .params("ex_coin", ex_coin)
                .params("num", num)
                .params("tpwd", tpwd)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    protected void _onSuccess(Response<HttpData> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getStatus() == 200) {
                            mView.submitSuccess();
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }
                });
    }
}
